<?php
class ArticleModel extends Model{
	public $_table='article_category';

	// 添加图片文章
	public function addPicArticle($id=false){
		$fp = FileUpload::getInstance();
		$fp->target_path = UPLOAD_ROOT;
		$upload_files = $fp->upload_file($_FILES['userfile']);

		$cateId = intval($_POST['category']);
		$data = $this->getOneData($cateId);


		/*进入附件表{{{*/
		$fs = Core::getInstance()->app_load_class('FileSystem');
		foreach($upload_files['original_uploadfile'] as $k=>$v){
			$arr = array(
				'`oldfile`'=>"'{$v}'",
				'`newfile`'=>"'{$upload_files['uploadfile'][$k]}'",
				'`thumb`'=>"'{$upload_files['thumb_uploadfile'][$k]}'",
			);
			$fs->addDb($arr);
		}
		/*}}}*/

		$this->_table = 'article';
		if($id){
			$id = intval($id);
			$db_data= $this->getOneData($id);
			$upload_files['original_uploadfile'] = array_merge($upload_files['original_uploadfile'],json_decode($db_data['original_uploadfile']));
			$upload_files['uploadfile'] = array_merge($upload_files['uploadfile'],json_decode($db_data['uploadfile']));
			$upload_files['thumb_uploadfile'] = array_merge($upload_files['thumb_uploadfile'],json_decode($db_data['thumb_uploadfile']));
		}
		
		$arr = array(
			'`title`'=>"'".Character::HTMLToDbEncode($_POST['title'])."'",
			'`categoryId`'=>$cateId,
			'`catename`'=>"'".$data['name']."'",
			'`original_uploadfile`'=>"'".json_encode($upload_files['original_uploadfile'])."'",
			'`uploadfile`'=>"'".json_encode($upload_files['uploadfile'])."'",
			'`thumb_uploadfile`'=>"'".json_encode($upload_files['thumb_uploadfile'])."'",
			'`create_time`'=>"'".date('Y-m-d H:i:s')."'",
			'`desc`'=>"'".Character::HTMLToDbEncode($_POST['desc'])."'",
			'`deschead`'=>"'".Character::subStr($_POST['desc'],300)."'",
		);

		if($id){
			$this->update($arr,'id='.$id,false);
		}else
			$this->add($arr);
	}

	public function modifyPicArticle(){
	}
	
	// 删除附件
	public function removeAttachment($id,$uploadfile){
		$id = intval($id);
		$uf = Character::HTMLToDbEncode($uploadfile);
		$this->_table = 'article';
		$o = $this->getOneData($id);
		if(!empty($o)){
			$db_original_uploadfile = json_decode($o['original_uploadfile']);
			$db_uploadfile = json_decode($o['uploadfile']);
			$db_thumb_uploadfile = json_decode($o['thumb_uploadfile']);

			if(empty($db_uploadfile)) return false;

			foreach($db_uploadfile as $k=>&$v){
				if(strpos($v,$uf) !== false){
					unset($db_uploadfile[$k],$db_thumb_uploadfile[$k],$db_original_uploadfile[$k]);
				}
			}

			$arr = array(
				'original_uploadfile'=>json_encode($db_original_uploadfile),
				'uploadfile'=>json_encode($db_uploadfile),
				'thumb_uploadfile'=>json_encode($db_thumb_uploadfile),
			);
			$this->update($arr,'id='.$id);
		}

	}


	/*分类管理{{{*/
	public function addCategory($arr){
		$name = Character::HTMLToDbEncode($_POST['name']);
		if(!empty($name)){
			$one = $this->getOneDataByName($name);
			if(!empty($one)){
				return false;
			}
			$arr = array(
				'`name`' => "'{$name}'"
			);
		}else{
			return false;
		}
		return $this->add($arr);
	}
	public function updateCategory($arr){
		$id = intval($arr['id']);
		$this->update(array('name'=>Character::HTMLToDbEncode($arr['name'])),'id='.$id);
	}
	public function getOneData($id){
		$sql = "select * from {$this->_table} where `id`=".intval($id);
		return $this->getOne($sql);
	}
	public function getOneDataByName($name){
		$sql = "select * from {$this->_table} where `name`='$name'";
		return $this->getOne($sql);
	}
	public function delData($id){
		$this->del($id);
	}

	public function modifyCategoryTpl($tpl,$id){
		$id = intval($id);
		$tpl = Character::HTMLToDbEncode($tpl);
	}
	/*}}}*/
}
